<template>
	<view>
		<view class="head">
			<view class="wait">
				<tui-icon name="clock" size="18" color="#fff"></tui-icon>
				<text class="text">等等付款</text>
			</view>
			<view class="need">
				<text>需要付款：{{data.payAmount}}元</text><text style="margin-left: 20rpx;">剩余：</text>
				<u-count-down :timestamp="time" :autoplay="true" @end="end()" separator="zh" bg-color="none" :show-days="false"></u-count-down>
			</view>
		</view>
		<view class="pay">
			<view class="pay_title">
				<text>支付方式</text>
				<text>支付金额：<text>{{data.payAmount}}</text></text>
			</view>

			<view class="pay_wx">
				<view class="left">
					<image class="img" src="/static/img/pay/icon_pay_weixin.png"></image>
					<text>微信支付</text>
				</view>

				<label class="tui-checkbox">
					<checkbox :checked="true" color="#fff" disabled></checkbox>
				</label>
			</view>

			<view class="pay_box tui-btn-box">
				<tui-button shape="circle" type="danger" @click="pay">去付款</tui-button>
			</view>

		</view>
		<u-modal ref="uModal" v-model="show" :show-cancel-button="true" :show-title="true" :async-close="false" @confirm="confirm"
		 content="是否完成支付" confirm-text="已支付" cancel-text="未支付" />

		<u-no-network tips="哎呀，网络信号丢失"></u-no-network>
		<failTimeout v-show="failTimeout"></failTimeout>
	</view>
</template>

<script>
	import OmsPortalOrderController from '../../api/OmsPortalOrderController.js'
	export default {
		data() {
			return {
				failTimeout: false,
				orderId: null,
				data: {},
				time: 0,
				show: false,
				payData: {},
				onShows: true,
			}
		},
		onLoad(o) {
			this.orderId = o.orderId
			

		},
		async onShow() {
			if (this.onShows) {
				await OmsPortalOrderController.detail(this.orderId).then(res => {
					if (res.data.code == 200) {
						if (res.data.data.status != 0) {
							uni.navigateBack()
							return;
						}
						this.time = (new Date(res.data.data.createTime).getTime() + 2 * 60 * 60 * 1000 - new Date().getTime()) / 1000
						this.data = res.data.data
						this.onShows = false;
					}
				}).catch(err => {
					if (err.errMsg == 'request:fail timeout') {
						this.failTimeout = true
					}
				})
			}

		},
		methods: {
			end() {
				uni.navigateBack()
			},
			async pay() {
				await this.requestSubscribeMessage()
				await OmsPortalOrderController.pay(this.data.id, 2).then(res => {
					if (res.data.code == 200) {
						this.payData = JSON.parse(res.data.data)
						let _this = this;
						uni.requestPayment({
							provider: 'wxpay',
							timeStamp: _this.payData.timeStamp,
							nonceStr: _this.payData.nonceStr,
							package: _this.payData.package,
							signType: _this.payData.signType,
							paySign: _this.payData.paySign,
							success: function(res) {
								setTimeout(() => {
									_this.onShows=true;
									uni.showLoading({
										mask: true,
										title: '支付中...'
									})
									OmsPortalOrderController.notice(_this.orderId).then(res=>{
										console.log(res)
									})
								}, 1)
								setTimeout(() => {
									uni.hideLoading()
									uni.navigateTo({
										url: '/pages/pay/paySuccess?orderId=' + _this.orderId
									})
								}, 1500)


							},
							fail: function(err) {
								setTimeout(() => {
									console.log(11123, err)
								}, 1)
							}
						});
					}

				}).catch(err => {
					console.log(err)
				})
				// ,
				// complete:e=>{
				// 	console.log(11133332,e)
				// 	if(e.errMsg=="stopMonitoringBeacons:ok"){
				// 		this.onShows=true;
				// 		uni.navigateTo({
				// 			url:'/pages/pay/paySuccess?orderId='+_this.orderId
				// 		})
				// 	}
				// }
			},
			async requestSubscribeMessage() {
				return new Promise((resolve, reject) => {
					uni.requestSubscribeMessage({
						tmplIds: ['Pr0IXWN0-okXLPJHxQbEGxtmx0eq8MtbQXe5tBk7kpc'],
						success: e => {
							resolve();

						},
						fail() {
							reject();
						}
					})
				})

			},
			pays() {

			}
		}
	}
</script>
<style>
	page {
		background-color: rgb(240, 240, 240);
	}
</style>

<style>
	/* #ifdef MP-WEIXIN */
	.tui-checkbox .wx-checkbox-input {
		width: 40rpx;
		height: 40rpx;
		margin-right: 0 !important;
		border-radius: 50% !important;
		transform: scale(0.8);
		border-color: #d1d1d1 !important;
	}

	.tui-checkbox .wx-checkbox-input.wx-checkbox-input-checked {
		background: #eb0909;
		width: 44rpx !important;
		height: 44rpx !important;
		border: none;
	}

	/* #endif */
	/* #ifndef MP-WEIXIN */

	>>>.tui-checkbox .uni-checkbox-input {
		width: 40rpx;
		height: 40rpx;
		margin-right: 0 !important;
		border-radius: 50% !important;
		transform: scale(0.8);
		border-color: #d1d1d1 !important;
	}

	>>>.tui-checkbox .uni-checkbox-input.uni-checkbox-input-checked {
		background: #eb0909;
		width: 45rpx !important;
		height: 45rpx !important;
		border: none;
	}

	/* #endif */
</style>
<style scoped>
	.head {
		width: 100%;
		height: 300rpx;

		background-color: #ff5100;
		padding-top: 100rpx;
		background-image: url(../../static/img/dd_bj.jpg);
		background-size: 100%;
	}

	.head .wait {
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.head .need {
		margin-top: 20rpx;
		display: flex;
		align-items: center;
		justify-content: center;

	}

	.head .wait .text {
		font-size: 30rpx;
	}

	.pay {
		background: #fff;
		border-radius: 20rpx;
		margin-top: 30rpx;
		padding: 40rpx;
	}

	.pay .pay_title {
		margin: 20rpx;
		padding-bottom: 20rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		border-bottom: 1px solid #dcdcdc;
	}

	.pay .pay_wx {
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-size: 30rpx;
		margin: 40rpx 0;
	}

	.pay .pay_wx .left {
		display: flex;
		align-items: center;
	}

	.pay .pay_wx .left .img {
		width: 50rpx;
		height: 50rpx;
		margin-right: 10rpx;
	}

	.pay .pay_box {
		margin-top: 100rpx;
	}
</style>
